package com.ant.storage.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ant.storage.pojo.UserInfo;
import org.apache.commons.logging.LogFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * @author hzd
 * @version 1.0
 * @date 2023/7/19 11:12
 */
@Component
public class UserInfoUtil {

    public static ServletContext servletContext;

    /**
     * 返回城市
     */
    public static ArrayList getCity(String str) {
        JSONObject jsonObject1 = JSON.parseObject(str);
        Object city = jsonObject1.get("city");
        JSONArray jsonArray = JSON.parseArray(city.toString());
        ArrayList list = new ArrayList();
        if (jsonArray.size() > 0) {
            jsonArray.stream().map(item -> {
                JSONObject object = JSON.parseObject(item.toString());
                Integer id = (Integer) object.get("id");
                list.add(id);
                return list;
            }).collect(Collectors.toList());
        }
        return list;
    }

    /**
     * 获取角色
     * @param str
     * @return
     */
    public static ArrayList getRole(String str) {
        JSONObject jsonObject1 = JSON.parseObject(str);
        Object city = jsonObject1.get("role");
        JSONArray jsonArray = JSON.parseArray(city.toString());
        ArrayList list = new ArrayList();
        if (jsonArray.size() > 0) {
            jsonArray.stream().map(item -> {
                JSONObject object = JSON.parseObject(item.toString());
                String id = (String) object.get("name");
                list.add(id);
                return list;
            }).collect(Collectors.toList());
        }
        return list;
    }

    public static String getUser(String str) {
        JSONObject jsonObject1 = JSON.parseObject(str);
        Object city = jsonObject1.get("id");
        return city.toString();
    }

    public static String getUserName(String str) {
        JSONObject jsonObject1 = JSON.parseObject(str);
        Object userName = jsonObject1.get("name");
        return userName.toString();
    }

    public static Integer getUserId(String str) {
        JSONObject jsonObject1 = JSON.parseObject(str);
        Object id = jsonObject1.get("id");
        return Integer.valueOf((Integer) id);
    }

    public static Integer getUserIds() {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        return userInfo.getId();
    }
    public static Integer getUserCityId() {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        System.out.println(userInfo);
        return userInfo.getCityId();
    }
}
